package me.shurufa.controller;

import android.support.v7.widget.RecyclerView;

import java.util.List;

import me.shurufa.bean.Book;
import me.shurufa.bean.Excerpt;
import me.shurufa.utils.Utils;

/**
 * Create by andy (https://github.com/andyxialm)
 * Create time: 16/5/23 16:50
 * Description :
 */
public class ExcerptCommentCounterController {
    private RecyclerView.Adapter mAdapter;
    private List<Excerpt> mDataList;
    private int mExcerptId;

    public ExcerptCommentCounterController(RecyclerView.Adapter adapter, List<Excerpt> dataList, int excerptId) {
        mAdapter = adapter;
        mDataList = dataList;
        mExcerptId = excerptId;
    }

    public void handleAddCounter() {
        try {
            if (Utils.isNullForList(mDataList)) {
                return;
            }

            if (mExcerptId == 0) {
                return;
            }

            // 遍历查询所有书，匹配id
            for (Excerpt excerpt : mDataList) {
                if (mExcerptId == excerpt.id) {
                    // 已查询到，判断是书摘 还是 书评, 做 +1 操作
                    int comment_num = excerpt.comment_num;
                    comment_num += 1;
                    excerpt.comment_num = comment_num;
                    break;
                }
            }
            mAdapter.notifyDataSetChanged();
        } catch (Exception e) {
        }
    }

}
